# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libjaylink
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=r175.cfccbc9
pkgrel=2
pkgdesc="Shared library written in C to access SEGGER J-Link and compatible devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://gitlab.zapb.de/libjaylink/libjaylink"
license=("GPLv2+")
depends=("${MINGW_PACKAGE_PREFIX}-libusb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "git")
options=('staticlibs' 'strip')
_commit="cfccbc9d6763733f1d14dff3c2dc5b75aaef136b"
source=("git+https://gitlab.zapb.de/libjaylink/libjaylink.git#commit=${_commit}"
        "010-socket.patch")
sha256sums=('SKIP'
            '260dba4fe44e8fc9a11eaa712cfcaa133d2e7386f25127b8b5e0e24169c4d027')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "r%s.%s" "$(git rev-list --count "${_commit}")" "$(git rev-parse --short "${_commit}")"
}

prepare() {
  cd ${_realname}
  patch -p1 -i ${srcdir}/010-socket.patch
  ./autogen.sh
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 ${srcdir}/${_realname}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
